package com.example.gymmanagement.controller;

import com.example.gymmanagement.Utils.Result;
import com.example.gymmanagement.Utils.StatusCode;
import com.example.gymmanagement.common.QueryPageParam;
import com.example.gymmanagement.domain.Field;
import com.example.gymmanagement.domain.User;
import com.example.gymmanagement.service.FieldService;
import com.example.gymmanagement.service.UserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
/**
*场地管理
 */
@RestController
@RequestMapping("/field")
public class FieldController {
    @Autowired
    private FieldService fieldService;
    @Autowired
    private UserService userService;

    @ApiOperation("查询场地（分页）")
    @GetMapping("/listPage")
    public Result listPage(int index,int size,Field field,@RequestHeader("Authorization") String token){
        Result fieldList = this.fieldService.selectList(index,size,field);
        return fieldList;
    }

    @ApiOperation("增加场地")
    @PostMapping("/save")
    public Result save(@RequestBody Field field,@RequestHeader("Authorization") String token){
        field.setFieldCreatetime(LocalDateTime.now());
        field.setFieldDetail("闲置中");
        boolean flag = fieldService.insert(field);
        String msg = flag?"添加场地成功":"添加场地失败";
        return new Result(flag? StatusCode.SAVE_OK:StatusCode.SAVE_ERR,field,msg);
    }
    @ApiOperation("按id删除场地")
    @GetMapping("/deleteById")
    public Result deleteById(int fieldId,@RequestHeader("Authorization") String token){
        boolean flag = fieldService.deleteById(fieldId);
        String msg = flag?"删除场地成功":"删除场地失败";
        return new Result(flag? StatusCode.DELETE_OK:StatusCode.DELETE_ERR,"操作场地id为"+fieldId,msg);
    }

    @ApiOperation("修改场地")
    @PostMapping("/updateField")
    public Result updateField(@RequestBody Field field,@RequestHeader("Authorization") String token){
        field.setFieldUpdatetime(LocalDateTime.now());
        boolean flag = fieldService.updateField(field);
        String msg = flag?"修改场地成功":"修改场地失败,id不存在";
        return new Result(flag? StatusCode.UPDATE_OK:StatusCode.UPDATE_ERR,field,msg);
    }

    @ApiOperation("预约场地")
    @GetMapping("/booking")
    public Result booking(int fieldId,int timeSize,int light,@RequestHeader("Authorization") String token){
        Result result = this.userService.selectCurrentUser(token);
        User user = (User) result.getData();
        Long userNum = user.getUserNum();
        LocalDateTime fieldBookingTime=LocalDateTime.now();
        long fieldId2 = fieldId;
        return  fieldService.booking(fieldId2,userNum,fieldBookingTime,timeSize,light);
    }

    @ApiOperation("退订场地")
    @GetMapping("/unBooking")
    public Result unbooking(int fieldId,@RequestHeader("Authorization") String token){
        long fieldId2 = fieldId;
        Boolean flag = fieldService.unBooking(fieldId2);
        String msg = (flag?"退订场地成功":"场地订单不存在");
        return new Result(flag? StatusCode.DELETE_OK:StatusCode.DELETE_ERR,flag,msg);
    }

    @ApiOperation("场地预约修改")
    @GetMapping("/upDateBooking")
    public Result upDateBooking(int fieldId,int time,@RequestHeader("Authorization") String token){
        Result result = this.userService.selectCurrentUser(token);
        User user = (User) result.getData();
        Long userNum = user.getUserNum();
        long fieldId2 = fieldId;
        return fieldService.upDateBooking(fieldId2,time,userNum);

    }
    @ApiOperation("归还场地")
    @GetMapping("/stopBooking")
    public Result stopBooking(int fieldId,@RequestHeader("Authorization") String token){
        Result result = this.userService.selectCurrentUser(token);
        User user = (User) result.getData();
        Long userNum = user.getUserNum();
        long fieldId2 = fieldId;
        return fieldService.stopBooking(fieldId2,userNum);
    }
}
